package com.sinoglobal.ningxia.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import com.sinoglobal.ningxia.R;

/**
 * 
* @title: AbBaseAdapter.java 
* @package com.sinoglobal.app.adapter 
* @description: baseAdapter的封装类 子类实现setView和setViewHolder方法即可
* @author zrh
* @date 2014-11-18 下午12:01:47
 */
public abstract class AbBaseAdapter<T> extends BaseAdapter{
    protected List<T>data;
    protected Context context;
    private int layoutId;
    public void setData(List<T> data) {
		this.data = data;
		notifyDataSetChanged();
	}
    public void addData(List<T> data) {
    	if(this.data!=null){
    		this.data.addAll(data);
    		notifyDataSetChanged();
    	}
    }
    public void clearData() {
    	if(this.data!=null){
    		this.data.clear();
    		notifyDataSetChanged();
    	}
    }
    /**
     * 
    * @description <p>构造方法</p> 
    * @author zrh
    * @date 2014-11-18 下午12:00:44
    * @param context
    * @param layoutId item的布局
     */
    public AbBaseAdapter(Context context,int layoutId) {
		this.context=context;
		this.layoutId=layoutId;
	}
    /**
     * 
    * @description <p>构造方法</p> 
    * @author zrh
    * @date 2014-11-18 下午12:08:09
    * @param context
    * @param layoutId item的布局
    * @param data 数据源
     */
    public AbBaseAdapter(Context context,int layoutId,List<T>data) {
    	this.context=context;
    	this.data=data;
    	this.layoutId=layoutId;
    }
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return data==null?0:data.size();
	}

	@Override
	public T getItem(int position) {
		// TODO Auto-generated method stub
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    	if(convertView==null){
    		convertView=LayoutInflater.from(context).inflate(layoutId, null);
    	}
    	setView(position, convertView);
    	return convertView;
    }
    /**
     * 
    * @author zrh
    * @methods setView 
    * @description 给view填充数据，如果有给设置ViewHolder 则可以使用viewHolder
    * @date 2014-11-18 下午3:47:58
    * @param position
    * @param convertView 参数说明
     */
    abstract void setView(int position, View convertView);
}
